package com.tempjob.admin.controller;

import com.anwen.mongo.conditions.query.LambdaQueryChainWrapper;
import com.anwen.mongo.model.PageParam;
import com.tempjob.admin.security.annotation.AdminLogIgore;
import com.tempjob.admin.security.annotation.AdminPassportSSO;
import com.tempjob.common.businesss.mongo.service.SysAdminLogService;
import com.tempjob.common.dto.admin.QueryLogDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统操作日志
 *
 * @version V1.0
 */
@RequestMapping("/sys")
@Api(tags = "日志管理", value = "系统操作日志管理")
@RestController
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
public class SysLogController {

    @Resource
    private SysAdminLogService logService;

    @PostMapping("/logs")
    @ApiOperation(value = "分页查询系统操作日志接口")
    @AdminLogIgore
    @AdminPassportSSO
//    @LogAnnotation(title = "系统操作日志管理", action = "分页查询系统操作日志")
    public Object pageInfo(@RequestBody QueryLogDTO vo) {
        PageParam page = new PageParam(vo.getPage(), vo.getSize());
        LambdaQueryChainWrapper queryWrapper = logService.lambdaQuery();
        if (StringUtils.isNotBlank(vo.getOrder()) && StringUtils.isNotBlank(vo.getSort())) {
            if (vo.getSort().equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(vo.getOrder());
            } else {
                queryWrapper.orderByDesc(vo.getOrder());
            }
        } else {
            queryWrapper.orderByDesc("create_time");
        }
        queryWrapper.like(StringUtils.isNoneBlank(vo.getUsername()), "username", vo.getUsername());
        queryWrapper.like(StringUtils.isNoneBlank(vo.getOperation()), "operation", vo.getOperation());
        queryWrapper.gt(StringUtils.isNoneBlank(vo.getStartTime()), "create_time", vo.getStartTime());
        queryWrapper.lt(StringUtils.isNoneBlank(vo.getEndTime()), "create_time", vo.getEndTime());
        return logService.page(queryWrapper, page);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除日志接口")
//    @LogAnnotation(title = "系统操作日志管理", action = "删除系统操作日志")
    public Object deleted(@RequestBody List<String> logIds) {
        return logService.removeBatchByIds(logIds);
    }



}
